package club.xyes.zkh.ecommerce.security.service.impl;

import club.xyes.zkh.ecommerce.commons.entity.User;
import club.xyes.zkh.ecommerce.dao.mapper.UserMapper;
import club.xyes.zkh.ecommerce.security.entity.SecurityUser;
import club.xyes.zkh.ecommerce.security.service.UserDetailServiceAdapter;
import club.xyes.zkh.ecommerce.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * Create by 郭文梁 2019/4/15 0015 16:15
 * UserDetailServiceAdapterImpl
 * SpringSecurity框架的用户详情服务类
 *
 * @author 郭文梁
 * @data 2019/4/15 0015
 */
@Service
public class UserDetailServiceAdapterImpl extends BaseServiceImpl<User> implements UserDetailServiceAdapter {
    private final UserMapper mapper;

    @Autowired
    public UserDetailServiceAdapterImpl(UserMapper mapper) {
        super(mapper);
        this.mapper = mapper;
    }

    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
        User user = mapper.findByUsername(name);
        if (user == null) {
            throw new UsernameNotFoundException("Not found!");
        }
        return SecurityUser.from(user);
    }
}
